﻿using AutoMapper;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using WMS.FC.Domain.DomainObject.System;
using WMS.FC.Domain.PersistenceObject.Business;
using WMS.FC.Domain.Repository;
using WMS.FC.Domain.Service.Cell.Interface;
using WMS.FC.Domain.Service.System;

namespace WMS.FC.Domain.Service.Cell
{
    public class CheckCellExistService : ServiceBase, ICheckCellExistService
    {
        protected readonly ICheckCellBarcodeService checkCellBarcodeService;
        protected readonly ICellRepository cellRepository;

        public CheckCellExistService(
            ILogger<ServiceBase> logger,
            IOptions<WmsSettings> options,
            IMapper mapper,
            ICellRepository cellRepository,
            ICheckCellBarcodeService checkCellBarcodeService)
            : base(logger, options, mapper)
        {
            this.cellRepository = cellRepository;
            this.checkCellBarcodeService = checkCellBarcodeService;
        }

        public async Task<ActionResult<CellPO>> CheckCellExist(string cellBarcode, UserInfo userInfo)
        {
            var result = new ActionResult<CellPO>("校验电池是否登录");
            try
            {
                var checkCellBarcodeResult = await checkCellBarcodeService.CheckCellBarcode(cellBarcode);
                if (!checkCellBarcodeResult.IsSuccess)
                    return result.Fail(checkCellBarcodeResult);

                var cellPO = await cellRepository.GetCellByBarcode(cellBarcode);
                if (cellPO == null)
                    return result.PartSuccess($"电池:{cellBarcode},未登录");

                result.Data = cellPO;
            }
            catch (Exception ex)
            {
                return result.Exception(ex);
            }

            return result;
        }
    }
}
